package cn.gok;

import java.util.ArrayList;
import java.util.Random;

class Pet{
    private String name;
    private int feetCount;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getFeetCount() {
        return feetCount;
    }

    public void setFeetCount(int feetCount) {
        this.feetCount = feetCount;
    }

    public Pet(String name, int feetCount) {
        this.name = name;
        this.feetCount = feetCount;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", feetCount=" + feetCount +
                '}';
    }
}
class Family{

    private String owner;
    private ArrayList<Pet> pets;

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public ArrayList<Pet> getPets() {
        return pets;
    }

    public void setPets(ArrayList<Pet> pets) {
        this.pets = pets;
    }

    public Family(String owner, ArrayList<Pet> pets) {
        this.owner = owner;
        this.pets = pets;
    }

    @Override
    public String toString() {
        return "Family{" +
                "owner='" + owner + '\'' +
                ", pets=" + pets +
                '}';
    }
}

//3  【提高】创建类 Pet: String name(宠物的名字) ,int feetCount（脚的个数）
//
//        设计类Family：String owner（主人），此外，每个家庭可以养有多只宠物（使用什么样的集合去存储宠物？？）。
//
//        a 创建2个family对象，分别拥有2只和3只宠物
//
//        b 写静态方法  ：   public static switchPets(Family f1,Famliy f2):  将两个家庭 脚最多的宠物进行互换。并输出互换后的结果。
//        特别注意：如果有多只宠物的脚条数一样，任选一只进行调换即可。
public class homeclass5_2 {
    public static void main(String[] args) {
        Pet pet = new Pet("狗",4);
        Pet pet1 = new Pet("猫",4);
        Pet pet2 = new Pet("猪",4);
        Pet pet3 = new Pet("兔子",4);
        Pet pet4 = new Pet("鸭",2);

        ArrayList<Pet> petList = new ArrayList<>();
        ArrayList<Pet> petList1 = new ArrayList<>();
        petList.add(pet);
        petList.add(pet1);
        petList1.add(pet2);
        petList1.add(pet3);
        petList1.add(pet4);

        Family family = new Family("a",petList);
        Family family1 = new Family("b",petList1);

        System.out.println(family);
        System.out.println(family1);

        switchPets(family,family1);

       System.out.println(family);
        System.out.println(family1);
    }



    public static void switchPets(Family f1, Family f2){
        ArrayList<Pet> pets1=f1.getPets();
        ArrayList<Pet> pets2=f2.getPets();

        ArrayList <Pet> pets3=findMax(pets1);
        ArrayList <Pet> pets4=findMax(pets2);

        int random = new Random().nextInt((pets3.size()-1));
        int random1=new Random().nextInt((pets4.size()-1));
        Pet pet1 = pets3.get(random);
        pets1.remove(pet1);
        Pet pet2 = pets4.get(random1);
        pets2.remove(pet2);

        pets2.add(pet1);
        pets1.add(pet2);
        f1.setPets(pets1);
        f2.setPets(pets2);

        }
    public  static ArrayList<Pet> findMax(ArrayList<Pet>  pets) {
        int foot = 0;
        ArrayList<Pet> pets1=new ArrayList<>();
        for (Pet pet : pets) {
            if (pet.getFeetCount() >= foot) {
                foot = pet.getFeetCount();
                pets1.add(pet);
            }
        }
        return pets1;
    }
}